package com.xiaomai.eduorder.controller;


import com.xiaomai.commonutil.R;
import com.xiaomai.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@RestController

@RequestMapping("/eduorder/paylog")

public class PayLogController {
    @Autowired
    private PayLogService logService;

    //1.创建付款二维码
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo){
      Map map = logService.createNative(orderNo);
        System.out.println("*****二维码map集合:"+map);
        return R.ok().data(map);
    }

    //2.查询订单状态
    @GetMapping("queryPayStatus/{orderNo}")
    private R queryPayStatus(@PathVariable String orderNo){
        Map<String,String> map =  logService.queryPayStatus(orderNo);//根据订单号查询订单状态
        System.out.println("*****查询订单状态map集合:"+map);
        if (map == null) {//出错
            return R.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//如果成功
            //更改订单状态
            logService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");


    }

}

